<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page errorPage="errorpage.jsp?from=register.jsp" %>
<%@ page import="com.cseg674.dataquery.authentication.Constants"%>
<jsp:useBean id="signIn" scope="session" class="com.cseg674.dataquery.authentication.SignIn" />
<jsp:useBean id="prof" scope="page" class="com.cseg674.dataquery.authentication.Profile" />
<jsp:setProperty name="prof" property="*" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%@ include file="includes/head.jsp" %>

<body>

<div id="page">

	<%@ include file="includes/header.jsp" %>
	
	<div id="menulinks">
		<a class="active" href="index.jsp"><span>Home</span></a>	
		<div class="menulines"></div>
		<a href="about.jsp"><span>About</span></a>
		<div class="menulines"></div>
		<a href="entry.jsp"><span>Entry</span></a>
		<div class="menulines"></div>
		<a href="query.jsp"><span>Query</span></a>
		<div class="menulines"></div>
		<a href="login.jsp"><span>Login</span></a>
		<div class="menulines"></div>
		<a href="engineers.jsp"><span>Engineers</span></a>
		<div class="menulines"></div>
	</div>

	<div id="mainarea">
		<div id="contentarea">
			<%
				if (request.getParameter("add")!=null){
					if (!prof.isValid()){
		    			out.println("some of the form entrys whith '*' not completed");
						return;
				}
				if (prof.EmailExist()){
			%>
				<jsp:include page="error.jsp?error_info=email already exist please use another one!" flush="true"/>
			<%
					return;
				}
				int rs=prof.insert();
			%>
				<center>
					<br><br><br>Login successful!<br>
					<a href="login.jsp"><font color="blue">Login Page</font></a>
					<br><br>
				</center>
		    <%
		    	}else if (request.getParameter("edit")!=null){
					if(!signIn.SignIn()){
						out.println("No login");
						return;
					}
					prof.update();
			%>
			<center>
				<br><br><br>Profile update sucessful!<br>
				<input class="button" type="button"  value="Close" onclick="Javascript:window.close();">
				<br><br>
			</center>
			<%
				}else{
			%>
	
				<script>
				<!--
				function validate(field) {
				   var valid = "_-0123456789";
				   var ok = "yes";
				   var temp;
				   for (var i=0; i<field.value.length; i++) {
				       temp = "" + field.value.substring(i, i+1);
				       if (valid.indexOf(temp) == "-1")
					      ok = "no";
				   }
				   if (ok == "no") {
				       alert(" must input digitals!");
				       field.focus();
				       field.select();
				   }
				}
				
				function Juge(theForm)
				{
				   var myRegExp = /\w+\@[.\w]+/;
				   if (!myRegExp.test(theForm.email.value)) {
						alert("Please input right email");
						theForm.email.focus();
						return false;
					}
				   if (theForm.firstname.value == "")
				   {
				   alert("Please enter your first name");
				   theForm.firstname.focus();
				   return (false);
				   }
				   if (theForm.lastname.value == "")
				   {
				   alert("Please enter your last name");
				   theForm.lastname.focus();
				   return (false);
				   }
				   if (theForm.groupname.value == "")
				   {
				   alert("Please enter your group name");
				   theForm.groupname.focus();
				   return (false);
				   }

				   if (theForm.secretcode.value != "cseg674")
				   {
				   alert("Ooops! Your secret code is invalid. Please try again :o)");
				   theForm.secretcode.focus();
				   return (false);
				   }
				   					
				  if (theForm.username.value == "")
				  {
				     alert("Please enter your username");
				     theForm.username.focus();
				     return (false);
				  }
				  if (theForm.password.value == "")
				  {
				  alert("Please enter your password");
				  theForm.password.focus();
				  return (false);
				  }
				  				  
				}
				
				-->
				</script>

				<form action="register.jsp" name=Form1 method=post onsubmit="return Juge(this);">
				<table width="100%" cellspacing=0 cellpadding=0 border=0>
					<table width="100%" cellpadding=2 cellspacing=2 border=0 bgcolor=#ffffff >

				<%
					prof.setUsername(signIn.getUsername());
					if (signIn.SignIn() && prof.select()){
				%>
						<tr>
							<td><p align="right">Username</p></td>
							<td><%=signIn.getUsername()%></td>
						</tr>
           				<input class="button" type="hidden" name="username" value="<%=signIn.getUsername()%>">
						<input class="button" type="hidden" name="password" value="<%=signIn.getPassword()%>">
						<input class="button" type="hidden" name="edit" value="1">
				<%
					}else{
				%>
					<tr>
						<td><p align="right">Username</p></td>
						<td><input class="button" name="username" value="" size=10></td>
					</tr>
					<tr>
						<td><p align="right">Password:</p></td>
						<td><input class="button"  name="password" type="password" value="" size=10>
					<tr>
					</tr>
					<input class="button" type="hidden" name="add" value="1">
				<%
					}
				%>
						</td>
					</tr>
					<tr>
						<td>&nbsp; </td>
						<td>&nbsp; </td>
					</tr>
					<tr bordercolor="#FFFFFF" bgcolor="#CCCCCC">
						<td colspan="2"><div align="center"><font color="#000000">Personal profile</font> </div></td>
					</td>
					<tr>
						<td><p align="right">Email</p></td>
						<td><input class="button" name="email" value="<%=prof.getEmail()%>">*</td>
					</tr>
					<tr>
						<td><p align="right">Firstname</p></td>
						<td><input class="button" name="firstname" value="<%=prof.getFirstname()%>">*</td>
					</tr>
					<tr>
						<td><p align="right">Lastname</p></td>
						<td><input class="button" name="lastname" value="<%=prof.getLastname()%>">*</td>
					</tr>
					<tr>
						<td><p align="right">Groupname</p></td>
						<td><input class="button" name="groupname" value="<%=prof.getGroupname()%>">*</td>
					</tr>
					<tr>
						<td><p align="right">Secret Code</p></td>
						<td><input class="button" name="secretcode"></td>
					</tr>
					<tr>
						<td>&nbsp; </td>
						<td><input class="button" type="submit" style='color: 000000; background-color:ededaf'  value="Submit">  <input class="button" type="Reset" style='color: 000000; background-color:ededaf'  value="Reset"></td>
					</tr>
					<tr>
						<td>&nbsp; </td>
						<td>&nbsp; </td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>
		<%
		}
		%>

		</div>

		<%@ include file="includes/sidebar.jsp" %>
		<%@ include file="includes/footer.jsp" %>
	</div>

</body>

</html>
